Skip to content

refactor(gemm): split the gemv_q8 grab-bag by family - #24

Merged
TheTom merged 2 commits into
devfrom
ek/kernel-reorg-gemv-q8-split
Jun 22, 2026
Merged

refactor(gemm): split the gemv_q8 grab-bag by family#24
TheTom merged 2 commits into
devfrom
ek/kernel-reorg-gemv-q8-split

Conversation

@ekryski

@ekryski ekryski commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Stacked on #23. Dissolves the last parked grab-bag from the gemm migration.

ffai/gemv_q8.rs bundled 25 kernels spanning 6 families in one 1069-line file. This splits each kernel to its proper family home.

→ Home Kernels
gemmkernels/gemm/gemv_quantized.rs (12 + 4 benches) Q8/Q4 inline-dequant GEMV: plain/_coalesced/_relu2/_accum, grouped(_rows,_tiled), Q4 _vec/_2row
moenew kernels/moe/ (5) gather_q4.rs (batched expert up/down/weighted-sum) + sigmoid_bias.rs (router pre-score)
opskernels/ops/ (4) cast_f32_f16/cast_f16_f32/vscale (unary.rs), slice (copy.rs)
ssmkernels/ssm/scan.rs (2) softplus_add, gated_group_rmsnorm (next to _rows/_batched siblings)
convolutionkernels/convolution/conv1d_causal.rs (1) conv_roll (causal-conv state roll)
kv_cachekernels/kv_cache/cache.rs (1) kv_append

Notes

  • All ffai_mt_. No kernels deleted — only the 4 q8-gemv kernels have Rust consumers; the other 21 are runtime-dispatch/registry-bound (deleting them would break Swift-side dispatch by name).
  • moe/ is seeded early (folder created ahead of the wave-3 moe migration); the remaining ~30 moe_* files in ffai/ migrate then. moe_sigmoid_bias is not a dup of the existing moe_router_sigmoid_bias (different output shape).
  • Per-family insertion places new kernels next to existing siblings, before each file's test/bench module.

Verification

  • cargo build --workspace --bins
  • cargo clippy --workspace --all-targets --all-features -- -D warnings ✓ (clean)
  • tile buildall 1272 kernels codegen to MSL ✓; every relocated kernel registered under its mt_ name (tile inspect)
  • gemv_q8 correctness tests (5) ✓; sibling tests in the 5 recipient files (conv1d_causal/kv_cache/strided/softplus/copy/cast) all green

@TheTom

TheTom commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

@ekryski clean split, no kernel dropped, no bench-name drift. The body understates one thing: the 21 "not deleted, registry-bound" kernels were renamed ffai_*mt_*, since #[kernel] derives the registered name from the fn name, the dispatch-by-name strings flip for all of them. So this is only safe in lockstep with a binding regen on the consumer side; worth saying so. Approve.

@TheTom
TheTom force-pushed the ek/kernel-reorg-gemm-quantized branch from 2b24cef to 742dcea Compare June 22, 2026 21:27
ekryski added 2 commits June 22, 2026 16:27
gemv_q8.rs bundled 25 kernels spanning 6 families. Split each to its home:

  - gemm  (12 + 4 benches) -> kernels/gemm/gemv_quantized.rs: the Q8/Q4
    inline-dequant GEMVs (plain/coalesced/relu2/accum, grouped(_rows,_tiled),
    Q4 _vec/_2row)
  - moe   (5)  -> NEW kernels/moe/: gather_q4.rs (batched expert up/down/
    weighted-sum) + sigmoid_bias.rs (router pre-score). moe folder seeded
    early; the rest of the moe family migrates in wave 3.
  - ops   (4)  -> kernels/ops/: cast_f32_f16 / cast_f16_f32 / vscale (unary.rs),
    slice (copy.rs)
  - ssm   (2)  -> kernels/ssm/scan.rs: softplus_add, gated_group_rmsnorm
    (next to their _rows / _batched siblings)
  - conv  (1)  -> kernels/convolution/conv1d_causal.rs: conv_roll (state roll)
  - kv    (1)  -> kernels/kv_cache/cache.rs: kv_append

All ffai_ -> mt_. No kernels deleted (the 21 consumer-less kernels are
runtime-dispatch/registry-bound). Fixed the 3 gemv_q8 correctness-test imports.
Dissolves the last parked grab-bag from the gemm migration.
@TheTom
TheTom force-pushed the ek/kernel-reorg-gemv-q8-split branch from 3255164 to b86ce1e Compare June 22, 2026 21:27
@TheTom
TheTom changed the base branch from ek/kernel-reorg-gemm-quantized to dev June 22, 2026 21:27
@TheTom
TheTom merged commit b5f696c into dev Jun 22, 2026
8 of 10 checks passed
TheTom pushed a commit that referenced this pull request Jun 22, 2026
Move all 35 moe-family files from ffai/ + mlx/ into kernels/moe/ (which #24
seeded with gather_q4 + sigmoid_bias):

  - orchestration (ex moe.rs): router_topk + permute/unpermute + 10 gather_qmm
  - routers: router_topk_biased (ex dsv4_router_topk), router_sigmoid_bias,
    router_sqrtsoftplus, sigmoid_bias
  - mpp grouped BGEMM: mpp(+int8/bm8/bm64/×int8/×block_scaled) + mpp_shared
  - gguf-format expert matmul: bgemm_{q2k,iq2xxs,q4}_*, gemv_{rows,ws}_*, gather_*
  - down combine: down_swiglu_accum, down_weighted_sum_f16
  - expert-indexed + block-scaled: dequant_gemv_expert_indexed(_block_scaled),
    block_scaled (ex mlx/block_scaled_moe)

Filenames drop the redundant moe_ prefix (folder provides it); kernel names
keep mt_moe_*. Model-name purge: mt_dsv4_router_topk -> mt_moe_router_topk_biased
(distinct from the generic mt_moe_router_topk: selects by the biased score,
weights by the unbiased). Bare dequant_gemv_int4_expert_indexed -> mt_ prefix.
Fixed mpp_shared intra-imports and ~27 consumer test files (grouped/mixed
use-blocks included). Format-axis fold (§7) deferred.

orchestration.rs (~4k lines) moves whole here; split follows next.
TheTom pushed a commit that referenced this pull request Jun 22, 2026
Move all 35 moe-family files from ffai/ + mlx/ into kernels/moe/ (which #24
seeded with gather_q4 + sigmoid_bias):

  - orchestration (ex moe.rs): router_topk + permute/unpermute + 10 gather_qmm
  - routers: router_topk_biased (ex dsv4_router_topk), router_sigmoid_bias,
    router_sqrtsoftplus, sigmoid_bias
  - mpp grouped BGEMM: mpp(+int8/bm8/bm64/×int8/×block_scaled) + mpp_shared
  - gguf-format expert matmul: bgemm_{q2k,iq2xxs,q4}_*, gemv_{rows,ws}_*, gather_*
  - down combine: down_swiglu_accum, down_weighted_sum_f16
  - expert-indexed + block-scaled: dequant_gemv_expert_indexed(_block_scaled),
    block_scaled (ex mlx/block_scaled_moe)

Filenames drop the redundant moe_ prefix (folder provides it); kernel names
keep mt_moe_*. Model-name purge: mt_dsv4_router_topk -> mt_moe_router_topk_biased
(distinct from the generic mt_moe_router_topk: selects by the biased score,
weights by the unbiased). Bare dequant_gemv_int4_expert_indexed -> mt_ prefix.
Fixed mpp_shared intra-imports and ~27 consumer test files (grouped/mixed
use-blocks included). Format-axis fold (§7) deferred.

orchestration.rs (~4k lines) moves whole here; split follows next.
@ekryski
ekryski deleted the ek/kernel-reorg-gemv-q8-split branch July 13, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ignore-for-release Hidden from release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants